<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable style="width: 250px">
          <el-option
            v-for="item in classList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>


      <!-- <el-form-item label="辅导员" prop="counselor">
        <el-input
          v-model="queryParams.counselor"
          placeholder="请输入辅导员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="EmploymentTrackingStudentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="班级" align="center" prop="classDept.deptName" />
      <el-table-column label="辅导员" align="center" prop="counselor" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:EmploymentTrackingStudent:edit']">新增就业追踪信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改就业追踪对话框 -->
    <el-dialog :title="title" v-model="open" width="550px" append-to-body>
      <el-form ref="EmploymentTrackingStudentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号" readonly/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" readonly/>
        </el-form-item>
        <el-form-item label="班级ID" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入班级ID" readonly/>
        </el-form-item>
        <el-form-item label="辅导员" prop="counselor">
          <el-input v-model="form.counselor" placeholder="请输入辅导员" readonly/>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" readonly/>
        </el-form-item>
        <el-divider content-position="center">就业追踪信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddEmploymentTracking">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteEmploymentTracking">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="employmentTrackingList" :row-class-name="rowEmploymentTrackingIndex" @selection-change="handleEmploymentTrackingSelectionChange" ref="employmentTracking">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="联系月份" prop="contactMonth" width="150">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.contactMonth"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择联系月份">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="就业记录" prop="employmentRecord" width="240">
            <template #default="scope">
              <el-input v-model="scope.row.employmentRecord" placeholder="请输入就业记录" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="EmploymentTrackingStudent">
import { listEmploymentTrackingStudent, getEmploymentTrackingStudent, delEmploymentTrackingStudent, addEmploymentTrackingStudent, updateEmploymentTrackingStudent } from "@/api/system/EmploymentTrackingStudent";
import useUserStore from '@/store/modules/user';
import { listDept } from "@/api/system/dept";

const { proxy } = getCurrentInstance();
const { string } = proxy.useDict('string');

const EmploymentTrackingStudentList = ref([]);
const employmentTrackingList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedEmploymentTracking = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const userStore = useUserStore();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentId: null,
    name: null,
    classId: null,
    counselor: null,
  },
  rules: {
    studentId: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    counselor: [
      { required: true, message: "辅导员不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
const uid = userStore.id;

/** 查询就业追踪列表 */
function getList() {
  loading.value = true;
  listEmploymentTrackingStudent(
      queryParams.value,
      uid
    ).then(response => {
      EmploymentTrackingStudentList.value = response.rows;
      total.value = response.total;
      console.log("ddd",response);
      loading.value = false;
    });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentId: null,
    name: null,
    gender: null,
    ethnicity: null,
    politicalStatus: null,
    collegeId: null,
    majorId: null,
    classId: null,
    counselor: null,
    classAdvisor: null,
    careerCounselor: null,
    phoneNumber: null,
    qqNumber: null,
    email: null,
    originPlace: null,
    homeAddress: null,
    homePhone: null,
    supportTeacher: null
  };
  employmentTrackingList.value = [];
  proxy.resetForm("EmploymentTrackingStudentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加就业追踪";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _studentId  = row.studentId
  getEmploymentTrackingStudent(_studentId).then(response => {
    form.value = response.data;
    employmentTrackingList.value = response.data.employmentTrackingList;
    open.value = true;
    title.value = "修改就业追踪";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["EmploymentTrackingStudentRef"].validate(valid => {
    if (valid) {
      form.value.employmentTrackingList = employmentTrackingList.value;
      if (form.value.id != null) {
        updateEmploymentTrackingStudent(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEmploymentTrackingStudent(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除就业追踪编号为"' + _ids + '"的数据项？').then(function() {
    return delEmploymentTrackingStudent(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 就业追踪序号 */
function rowEmploymentTrackingIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 就业追踪添加按钮操作 */
function handleAddEmploymentTracking() {
  let obj = {};
  obj.contactMonth = "";
  obj.employmentRecord = "";
  employmentTrackingList.value.push(obj);
}

/** 就业追踪删除按钮操作 */
function handleDeleteEmploymentTracking() {
  if (checkedEmploymentTracking.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的就业追踪数据");
  } else {
    const employmentTrackings = employmentTrackingList.value;
    const checkedEmploymentTrackings = checkedEmploymentTracking.value;
    employmentTrackingList.value = employmentTrackings.filter(function(item) {
      return checkedEmploymentTrackings.indexOf(item.index) == -1
    });
  }
}

// 查询班级信息列表 */
// 封装响应式对象
const classParams = reactive({
    pageNum: 1, 
    pageSize: 1000,
    ancestors: 200
});
// 定义返回结果列表
const classList = ref([]);

// 调用方法
function getClassList() {
  listDept({ pageNum: classParams.pageNum, pageSize: classParams.pageSize, ancestors: classParams.ancestors}).then(response => {
    classList.value = response.data;
    console.log("classList",classList.value);
  })
}


/** 复选框选中数据 */
function handleEmploymentTrackingSelectionChange(selection) {
  checkedEmploymentTracking.value = selection.map(item => item.index)
}

// /** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/EmploymentTrackingStudent/export', {
//     ...queryParams.value
//   }, `学生就业跟踪_${new Date().getTime()}.xlsx`)
// }

getClassList();
getList();
</script>
